package ui
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	
	import texture.BitmapEx;

	public class GuiWnd extends Sprite
	{
		private var eventLIst:Dictionary = new Dictionary(true);
		
		protected var width_Value:int;
		protected var height_Value:int;
		
		public  var wndName:String;

		public function GuiWnd()
		{
			wndName = "";
		}
		
		public function init(xml:XML):void
		{
			
		}

		public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			var listenerDic:Dictionary;
			if(!this.eventLIst[type])
				this.eventLIst[type] = {useCap:new Dictionary(true),noCap:new Dictionary(true)};			
			listenerDic = useCapture ? this.eventLIst[type].useCap : this.eventLIst[type].noCap;
			listenerDic[listener] = true;
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		public override function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			if(!this.eventLIst[type])
				return;
			var listenerDic:Dictionary = useCapture ? this.eventLIst[type].useCap : this.eventLIst[type].noCap;
			delete listenerDic[listener];
			super.removeEventListener(type,listener,useCapture);
		}
		
		public function get nWidth():int
		{
			return width_Value;
		}
		
		public function set nWidth(value:int):void
		{
			width_Value = value;
		}
		
		public function get nHeight():int
		{
			return height_Value;
		}
		
		public function set nHeight(value:int):void
		{
			height_Value = value;
		}
		
		public function setPos(nX:int, nY:int):void
		{
			this.x = nX;
			this.y = nY;
		}

		public function destroy():void
		{
			clearAllChild(this)
			clearAllEvent();
		}
		
		public function clearAllChild(container:DisplayObjectContainer):void
		{
			if(container is MovieClip)
				MovieClip(container).stop();			
			while(container.numChildren > 0)
			{
				var child:DisplayObject = container.getChildAt(0);
				if(!child)
				{
					break;
				}
				container.removeChild(child);
				
				if(child is BitmapEx)
				{
					BitmapEx(child).dispose();
				}
				else if(child is Bitmap && Bitmap(child).bitmapData)
				{
					Bitmap(child).bitmapData.dispose();	
				}
				else if(child is Sprite)
				{
					clearAllChild(child as DisplayObjectContainer);
				}
				else if(child is MovieClip)
				{
					MovieClip(child).stop();
					clearAllChild(child as DisplayObjectContainer);
				}
				else if(child is DisplayObjectContainer)
					clearAllChild(child as DisplayObjectContainer);
				
				child = null;
			}
		}
		
		public function clearAllEvent():void
		{
			for(var type:String in this.eventLIst)
			{
				var eventObj:Object = this.eventLIst[type];
				for(var useCapfun:Object in eventObj.useCap)
				{
					delete eventObj.useCap[useCapfun];
					super.removeEventListener(type,useCapfun as Function,true);
				}
				for(var noCapfun:Object in eventObj.noCap)
				{
					delete eventObj.noCap[noCapfun];
					super.removeEventListener(type,noCapfun as Function,false);
				}
				delete eventLIst[type];
			}
		}
		

	}
}